<style>
.messageActivity .el-input.is-disabled .el-input__inner{
  color:black;
}
.messageActivity .el-textarea.is-disabled .el-textarea__inner{
  color:black;
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动标题" prop="subject">
        <el-input
          v-model="queryParams.subject"
          placeholder="请输入活动标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息模板编号" prop="templateNo">
        <el-input
          v-model="queryParams.templateNo"
          placeholder="请输入消息模板编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入消息模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户帐号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户帐号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          placeholder="请输入商户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="读取状态" prop="readStatus">
        <el-select v-model="queryParams.readStatus" placeholder="请选择读取状态" clearable size="small">
          <el-option
          v-for="dict in msgReadStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送状态" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择发送状态" clearable size="small">
          <el-option
          v-for="dict in msgSendStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['message:activity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['message:activity:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动标题" align="center" prop="subject" />
      <el-table-column label="消息模板编号" align="center" prop="templateNo" />
      <el-table-column label="消息模板名称" align="center" prop="templateName" />
      <el-table-column label="消息内容" align="center" prop="content" />
      <el-table-column label="用户帐号" align="center" prop="username" />
      <el-table-column label="商户名称" align="center" prop="merchantName" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="读取状态" align="center" prop="readStatus" :formatter="msgReadStatusFmt" />
      <el-table-column label="发送日期" align="center" prop="sendDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.sendDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发送状态" align="center" prop="sendStatus" :formatter="msgSendStatusFmt" />
      <el-table-column label="失败信息" align="center" prop="failedMsg" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['message:activity:query']"
          >详细</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['message:activity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动消息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动编号" prop="activityNo">
          <el-select v-model="form.activityNo" placeholder="请选择活动编号">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息模板编号" prop="templateNo">
          <el-select v-model="form.templateNo" placeholder="请选择消息模板编号">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入消息模板名称" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户编号" prop="customerNo">
          <el-input v-model="form.customerNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="商户编号" prop="merchantNo">
          <el-input v-model="form.merchantNo" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productNo">
          <el-input v-model="form.productNo" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="读取状态">
          <el-radio-group v-model="form.readStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发送日期" prop="sendDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sendDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发送日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发送状态">
          <el-radio-group v-model="form.sendStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="失败信息" prop="failedMsg">
          <el-input v-model="form.failedMsg" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标记">
          <el-radio-group v-model="form.deleteFlag">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form class="messageActivity" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动编号" prop="activityNo">
          <el-input disabled v-model="form.activityNo" placeholder="请输入活动编号" />
        </el-form-item>
        <el-form-item label="活动标题" prop="subject">
          <el-input disabled v-model="form.subject" placeholder="请输入活动标题" />
        </el-form-item>
        <el-form-item label="消息模板编号" prop="templateNo">
          <el-input disabled v-model="form.templateNo" placeholder="请输入消息模板编号" />
        </el-form-item>
        <el-form-item label="消息模板名称" prop="templateName">
          <el-input disabled v-model="form.templateName" placeholder="请输入消息模板名称" />
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input disabled v-model="form.content" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="用户编号" prop="customerNo">
          <el-input disabled v-model="form.customerNo" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="用户帐号" prop="username">
          <el-input disabled v-model="form.username" placeholder="请输入用户帐号" />
        </el-form-item>
        <el-form-item label="商户编号" prop="merchantNo">
          <el-input disabled v-model="form.merchantNo" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="商户名称" prop="merchantName">
          <el-input disabled v-model="form.merchantName" placeholder="请输入商户名称" />
        </el-form-item>
        <el-form-item label="产品编号" prop="productNo">
          <el-input disabled v-model="form.productNo" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input disabled v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="读取状态">
          <el-select disabled v-model="form.readStatus" placeholder="请选择读取状态">
            <el-option
            v-for="dict in msgReadStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送日期" prop="sendDate">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.sendDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择发送日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发送时间" prop="sendTime">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.sendTime"
            type="time"
            value-format="HHmmss"
            placeholder="选择发送时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发送状态">
          <el-select disabled v-model="form.sendStatus" placeholder="请选择发送状态">
            <el-option
            v-for="dict in msgSendStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.sendStatus==0&&form.failedMsg" label="失败信息" prop="failedMsg">
          <el-input disabled v-model="form.failedMsg" placeholder="请输入失败信息" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity, exportActivity, logicRemoveActivity, selectListActivityVo, getActivityVo } from "@/api/message/activity";

export default {
  name: "Activity",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 消息读取状态集合
      msgReadStatusOptions: [],
      // 消息发送状态集合
      msgSendStatusOptions: [],
      // 总条数
      total: 0,
      // 活动消息表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityNo: null,
        templateNo: null,
        templateName: null,
        content: null,
        customerNo: null,
        merchantNo: null,
        productNo: null,
        readStatus: null,
        sendDate: null,
        sendTime: null,
        sendStatus: null,
        failedMsg: null,
        deleteFlag: null,
        username: null,
        productName: null,
        subject: null,
        merchantName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("msg_message_read_status").then(response => {
      this.msgReadStatusOptions = response.data;
    });
    this.getDicts("msg_message_send_status").then(response => {
      this.msgSendStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询活动消息列表 */
    getList() {
      this.loading = true;
      selectListActivityVo(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        activityNo: null,
        templateNo: null,
        templateName: null,
        content: null,
        customerNo: null,
        merchantNo: null,
        productNo: null,
        readStatus: "0",
        sendDate: null,
        sendTime: null,
        sendStatus: "0",
        failedMsg: null,
        deleteFlag: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动消息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivityVo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动消息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateActivity(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      getActivityVo(row.id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "活动消息详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除活动消息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return logicRemoveActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动消息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportActivity(queryParams);
        }).catch(() => {});
    },
    /** 发送状态格式化 */
    msgSendStatusFmt(row, column) {
      var sendStatus = row.sendStatus;
      if(!sendStatus){
        return "";
      }
      var val = sendStatus.trim();
      this.msgSendStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 读取状态格式化 */
    msgReadStatusFmt(row, column) {
      var readStatus = row.readStatus;
      if(!readStatus){
        return "";
      }
      var val = readStatus.trim();
      this.msgReadStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 日期格式化 */
    dateFmt(str) {
      return str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8);
    }
  }
};
</script>
